/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bzflagai;

import ai.ClayPigeon;
import ai.Intelligence;
import ai.MultiAgent;
import ai.Reactive;
import ai.Searching;
import ai.Stupid;
import ai.Tournament;
import comm.Messenger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import world.World;

/**
 *
 * @author dheath
 */
public class Controller
{

    private Messenger msgr;
    private List<Intelligence> bots;
   
    public void run()
    {
        try
        {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader stdin = new BufferedReader(isr);


            //Get Host
            System.out.println("What is the IP Address of the host?\n");
            String host = stdin.readLine();

            //Get Port
            System.out.println("What port is used?\n");
            int port = Integer.parseInt(stdin.readLine());

            //Number of Bots
            System.out.println("How many tank bots?\n");
            int botNum = Integer.parseInt(stdin.readLine());
            
            //Number of Bots
            System.out.println("How much noise?\n");
            double noise = Double.parseDouble(stdin.readLine());
            
            //Messenger Initialize
            msgr = new Messenger(host, port);

            //Generate Intelligence
            bots = new ArrayList();

            for (int i = 0; i < botNum; i++)
            {
                while (true)
                {
                    System.out.println("Enter AI for bot: " + i);
                    System.out.println("1 - Stupid");
                    System.out.println("2 - Potential Fields");
                    System.out.println("3 - Searching");
                    System.out.println("4 - Multi-Agent");
                    System.out.println("5 - Clay Pigeons");
                    System.out.println("6 - Tournament");

                    String ai = stdin.readLine();
                    int learn = 0;
                    try
                    {
                        learn = Integer.parseInt(ai);
                    } catch (NumberFormatException ex)
                    {
                        System.out.println("choice must be a number from 1 to 6");
                        continue;
                    }
                    if (learn > 0 && learn < 7)
                    {
                        Intelligence bot = getIntelligence(learn, i,noise);
                        bot.setMessenger(msgr);
                        bots.add(bot);
                        break;
                    }
                }
            }

            stdin.close();
/////////////////////////////////////////////////////////////////////////////////////////////
            msgr.establishConnection();

            World world = msgr.buildWorld();
            for (Intelligence bot : bots)
            {
                bot.setWorld(world);
            }

            List<Thread> botThreads = new ArrayList();

            int c = 0;
            while (c++ < 10000)
            {
                msgr.updateWorld(world);

                int i = 0;
                for (Intelligence bot : bots)
                {
                    Thread th = new Thread(new Thinker(bot), "thread: " + i);
                    botThreads.add(th);

                    th.start();
                    i++;
                }

                boolean keepGoing = true;
                while (keepGoing)
                {
                    keepGoing = false;
                    for (Thread th : botThreads)
                    {
                        if (th.isAlive())
                        {
                            keepGoing = true;
                            try
                            {
                                Thread.sleep(50);
                            } catch (InterruptedException ex)
                            {
                                ex.printStackTrace();
                            }
                            break;
                        }
                    }
                }




            }

            msgr.endConnection();

        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private Intelligence getIntelligence(int type, int botId,double noise)
    {
        switch (type)
        {
            case 1:
                return new Stupid(botId);
            case 2:
                return new Reactive(botId);
            case 3:
                return new Searching(botId);
            case 4:
                return new MultiAgent(botId);
            case 5:
                return new ClayPigeon(botId);
            case 6:
                return new Tournament(botId,noise);
            default:
                return new Stupid(botId);
        }
    }
}
